<?php
namespace App\Controller;

use Think\Upload;

class IndexController extends BaseController {
	/**
	 * 接口入口
     */
	public function index()
	{
		$func = $this->method;
		$this->$func();
	}

	/**
	 * APP安装统计
	 */
	public function installCount()
	{
		$Installcount = D('Installcount');
		$status = $Installcount->installcount($this->client_plat,$this->client_version,$this->client_sign);
		if($status){
			$this->returnCode(1,'安装统计成功');
		}else{
			$this->returnCode(0,'安装统计失败');
		}
	}

	/**
	 * APP活跃统计
	 */
	public function activeCount()
	{
		$Activecount = D('Activecount');
		$status = $Activecount->activecount($this->client_plat,$this->client_sign);
		if($status){
			$Version = D('version');
			$data = $Version->update($this->client_plat,$this->client_version);
			if (empty($data)){
				// 无更新
				$this->returnCode(1,'活跃统计成功');
			}else{
				// 有更新
				$this->returnCode(2,'活跃统计成功',$data);
			}
		}else{
			$this->returnCode(0,'活跃统计失败');
		}
	}

	/**
	 * 获取引导图片
	 */
	public function getAppGuide()
	{
		$Guide = D('guide');
		$guidearr = $Guide->getappguide();
		if($guidearr){
			$this->returnCode(1,'获取引导图片成功',$guidearr);
		}else{
			$this->returnCode(1,'获取引导图片失败',[]);
		}
	}

	/**
	 * 获取分享数据
	 */
	public function getShareData()
	{
		$data = array(
			'tilte' => C('SHARE_TITLE'),
			'comment' => C('SHARE_COMMENT'),
			'link' => C('SHARE_LINK')
		);
		$this->returnCode(1,'获取分享数据成功',$data);
	}

	/**
	 * 用户注册
	 */
	public function uRegister()
	{
		$User = D('user');
		$u_status = $User->uregister($this->apiparams['phone'],$this->apiparams['codenum'],$this->apiparams['pass'],$this->client_sign,$this->client_plat);
		switch ($u_status){
			case 1:
				$this->returnCode(1,'注册成功');
				break;
			case 2:
				$this->returnCode(0,'该号码已注册');
				break;
			case 3:
				$this->returnCode(0,'验证码已失效');
				break;
			case 4:
				$this->returnCode(0,'验证码错误');
				break;
			case 5:
				$this->returnCode(0,'手机号码格式错误');
				break;
			default:
				$this->returnCode(0,'注册失败');
		}
	}

	/**
	 * 用户登录
	 */
	public function userLogin()
	{
		$User = D('user');
		$data = $User->userlogin($this->apiparams['phone'],$this->apiparams['pass'],$this->client_sign,$this->client_plat);
		if($data == -1) {
			$this->returnCode(0,'用户不存在');
		}elseif($data == -2) {
			$this->returnCode(0,'登录密码有误');
		}else{
			$return = array(
				'uid' => $data['id'],
				'push_code' => $data['push_code'],
				'account' => $data['account']
			);
			$this->returnCode(1,'登录成功',$return);
		}
	}

	/**
	 * 忘记密码
	 */
	public function uForgetPass()
	{
		$User = D('user');
		$status = $User->uforgetpass($this->apiparams['phone'],$this->apiparams['codenum'],$this->apiparams['pass']);
		switch($status){
			case 1:
				$this->returnCode(1,'修改密码成功');
				break;
			case 2:
				$this->returnCode(0,'验证码错误');
				break;
			case 3:
				$this->returnCode(0,'验证码已失效');
				break;
			default:
				$this->returnCode(0,'修改密码失败');
		}
	}

	/**
	 * 修改密码
	 */
	public function altUserPass()
	{
		$User = D('user');
		$status = $User->altuserpass($this->apiparams['uid'],$this->apiparams['oldpass'],$this->apiparams['newpass']);
		switch($status){
			case 1:
				$this->returnCode(1,'修改密码成功');
				break;
			case 2:
				$this->returnCode(0,'原密码错误');
				break;
			default:
				$this->returnCode(0,'修改密码失败');
		}
	}

	/**
	 * 用户充值
	 */
	public function userRecharge()
	{
		switch($this->client_plat){
			case 1:
				//IOS
				$code = 'IS'.uniqid().$this->apiparams['uid'];
				break;
			case 2:
				//安卓
				$code = 'AD'.uniqid().$this->apiparams['uid'];
				break;
			default:
				$this->returnCode(0,'平台参数异常');
		}

		$UserRecharge = D('UserRecharge');
		$data = $UserRecharge->userrecharge($this->apiparams['uid'],$this->apiparams['money'],$this->apiparams['type'],$code);

		if(!$data){
			$this->returnCode(0,'充值订单生成异常');
		}

		$type = $this->apiparams['type'];
		if($type == 1){
			$pay_url = C('ALI_PAY_RECHARGE');
			//支付宝
			$return = array(
				'title' => '充值支付',
				'returnpath' => $pay_url,
				'ordernum' => $data['code'],
				'money' => $data['money']
			);
			$this->returnCode(1,'用户充值订单生成成功',$return);
		}else{
			//微信
			$trade_type ='JSAPI';

			$app_id = C('WX_APP_ID');
			$secret = C('WX_SECRET');
			$mch_id = C('WX_MCH_ID');
			$pay_key = C('WX_PAY_KEY');
			$pay_url = C('WX_PAY_RECHARGE');

			$wx_pay = new \Org\Util\Wxpay($app_id, $secret, $mch_id, $pay_key, $pay_url, $trade_type);

			$param = array(
				'body' => 'user_recharge',            //订单描述
				'out_trade_no' => $data['code'],        //订单编号
				'total_fee' => $data['money']*100,      //订单金额
				'spbill_create_ip' => get_client_ip()   //IP
			);
			$res = $wx_pay->unified_pay($param);//生成预支付订单
			if($res['status'] == 0){
				$return = array(
					'return_code' => $res['return_code'],
					'return_msg' => $res['return_msg']
				);
				$this->returnCode(0,'微信预支付订单生成失败',$return);
			}else{
				$prepay_id = $res['prepay_id'];//生成预支付订单成功
			}
			$res_arr = $wx_pay->create_payparam($prepay_id);
			$this->returnCode(1,'用户充值订单生成成功',$res_arr);
		}
	}

	/**
	 * APP验证码
	 */
	public function verCode()
	{
		$data = sendCode($this->apiparams['phone'],false);//测试阶段不发送验证码
//		$data = sendCode($this->apiparams['phone'],true);//运营阶段发送验证码
		if($data){
			$this->returnCode(1,'验证码发送成功',$data);
//			$this->returnCode(1,'验证码发送成功');
		}else{
			$this->returnCode(0,'验证码发送失败');
		}
	}

	/**
	 * 获取省市区列表
	 */
	public function getAreaList(){
		$prov_model = M('s_province');
		$prov_field = 'ProvinceID,ProvinceName';
		$area = $prov_model->field($prov_field)->select();

		if(!empty($this->apiparams['province_id'])){
			$city_model = M('s_city');
			$city_map['ProvinceID'] = $this->apiparams['province_id'];
			$city_field = 'CityID,CityName,ProvinceID';
			$area = $city_model->field($city_field)->where($city_map)->select();
		}

		if (!empty($this->apiparams['city_id'])){
			$dist_model = M('s_district');
			$dist_map['CityID'] = $this->apiparams['city_id'];
			$dist_field = 'DistrictID,DistrictName,CityID';
			$area = $dist_model->field($dist_field)->where($dist_map)->select();
		}

		$this->returnCode(1,'区域列表',$area);
	}

	/**
	 * 获取省市区ID
	 */
	public function getAreaID(){
		//获取省ID
		$prov_model = M('s_province');
		$province_name = str_replace('省','',$this->apiparams['ProvinceName']);
		$province_name = str_replace('市','',$province_name);
		$prov_map['ProvinceName'] = array("like","%".$province_name."%");
		$prov = $prov_model->where($prov_map)->field('ProvinceID,ProvinceName')->find();

		//获取市ID
		$city_model = M('s_city');
		$city_name = str_replace('市','',$this->apiparams['CityName']);
		$city_map['CityName'] = array("like","%".$city_name."%");
		$city_map['ProvinceID'] = array('eq',$prov['ProvinceID']);
		$city = $city_model->where($city_map)->field('CityID,CityName,ProvinceID')->find();

		//获取区ID
		$dist_model = M('s_district');
		$district_name = str_replace('县','',$this->apiparams['DistrictName']);
		$district_name = str_replace('区','',$district_name);
		$dist_map['DistrictName'] = array("like","%".$district_name."%");
		$dist_map['CityID'] = array('eq',$city['CityID']);
		$dist = $dist_model->where($dist_map)->field('DistrictID,DistrictName,CityID')->find();

		$area = array(
			'province' => $prov?$prov:'',
			'city' => $city?$city:'',
			'district' => $dist?$dist:'',
		);
		if(!empty($area)){
			$this->returnCode(1,'请求成功',$area);
		}else{
			$this->returnCode(0,'参数有误');
		}
	}

	/**
	 * 编辑用户地址
	 */
	public function editUserAddress(){

		$model = D('address');
		if(empty($this->apiparams['address_id'])){
			$address_status = $model->add_user_address($this->apiparams['user_id'],$this->apiparams['name'],$this->apiparams['phone'],$this->apiparams['select_address'],$this->apiparams['address'],$this->apiparams['longitude'],$this->apiparams['latitude'],$this->apiparams['province_id'],$this->apiparams['city_id'],$this->apiparams['district_id']);
		}else{
			$address_status = $model->edit_user_address($this->apiparams['address_id'],$this->apiparams['user_id'],$this->apiparams['name'],$this->apiparams['phone'],$this->apiparams['select_address'],$this->apiparams['address'],$this->apiparams['longitude'],$this->apiparams['latitude'],$this->apiparams['province_id'],$this->apiparams['city_id'],$this->apiparams['district_id']);
		}
		switch($address_status){
			case 1:
				$this->returnCode(1,'保存成功');
				break;
			case 2:
				$this->returnCode(0,'无权编辑');
				break;
			default:
				$this->returnCode(0,'保存失败');
		}
	}

	/**
	 * 获取用户地址簿
	 */
	public function getAddressList(){
		$model = D('address');
		$address_arr = $model->get_address_list($this->apiparams['user_id']);
		$this->returnCode(1,'用户地址列表',$address_arr);
	}

	/**
	 * 删除用户地址
	 */
	public function delUserAddress(){
		$model = D('address');
		$del_res = $model->del_user_address($this->apiparams['user_id'],$this->apiparams['address_id']);
		switch($del_res){
			case 1:
				$this->returnCode(1,'删除成功');
				break;
			case 2:
				$this->returnCode(0,'无权删除');
				break;
			default:
				$this->returnCode(0,'删除失败');
		}
	}

	/**
	 * 设置用户默认地址
	 */
	public function defaultUserAddress(){
		$model = D('address');
		$address_status = $model->default_user_address($this->apiparams['user_id'],$this->apiparams['address_id']);
		switch($address_status){
			case 1:
				$this->returnCode(1,'设置成功');
				break;
			case 2:
				$this->returnCode(0,'无权更改');
				break;
			default:
				$this->returnCode(0,'设置失败');
		}
	}

	/**
	 * 司机银行卡列表
	 */
	public function bankList(){
		$model = D('user_bank');
		$bank_arr = $model->bank_list($this->apiparams['user_id']);
		$this->returnCode(1,'银行卡列表',$bank_arr);
	}

	/**
	 * 获取银行卡区域列表
	 */
	public function bankAreaList(){
		$model = D('bank_area');
		$area_data = $model->bank_area_list($this->apiparams['parent_id']);
		$this->returnCode(1,'银行卡区域列表',$area_data);
	}

	/**
	 * 司机绑定银行卡
	 */
	public function bandBankCard(){
		$model = D('user_bank');
		$band_status = $model->band_bank_card($this->apiparams['bank_number'],$this->apiparams['user_id'],$this->apiparams['true_name'],$this->apiparams['bank_name'],$this->apiparams['branch_name'],$this->apiparams['province_id'],$this->apiparams['city_id']);
		if($band_status) {
			$this->returnCode(1,'银行卡编辑成功');
		}else{
			$this->returnCode(0,'银行卡编辑失败');
		}
	}

	/**
	 * 司机删除银行卡
	 */
	public function delBankCard(){
		$model = D('user_bank');
		$del_status = $model->del_bank_card($this->apiparams['card_id']);
		if($del_status){
			$this->returnCode(1,'删除银行卡成功');
		}else{
			$this->returnCode(0,'删除银行卡失败');
		}
	}

	/**
	 * 图片上传
	 */
	public function uploadImg(){
		$upload = new Upload();                             //实例化上传类
		$upload->maxSize = 10485760;                        //设置附件上传大小(修改为10M)
		$upload->exts = array('jpg','gif','png','jpeg');    //设置附件上传类型
		$upload->rootPath = './Uploads/';                   //设置附件上传根目录
		$upload->savePath = '';                             //设置附件上传（子）目录
		$upload->saveName = '';                             //设置保存文件名
		$upload->replace = true;                            //同名文件是否覆盖
		$upload->autoSub = true;                            //自动使用子目录保存上传文件
		$upload->subName = 'picture'.rand(100,999);          //子目录创建方式

		//上传文件
		$upload_arr = $upload->upload();
		$upload_err = $upload->getError();
		if(!empty($upload_err)){
			$this->returnCode(0,'图片上传失败');
		}else{
			foreach($upload_arr as $k => $v){
				$path = '/Uploads/'.$v['savepath'].$v['savename'];
				$return[$k] = $path;
			}
			$this->returnCode(1,'图片上传成功',$return);
		}
	}

	/**
	 * 获取用户余额
	 */
	public function getUserAccount(){
		$model = D('user');
		$account = $model->get_user_account($this->apiparams['user_id']);
		$this->returnCode(1,'用户余额',$account);
	}

	
}